package com.cuanbobox.adapter;

import android.os.Environment;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cuanbobox.activity.MainActivity;
import com.cuanbobox.activity.PreviewActivity;
import com.cuanbobox.base.App;
import com.cuanbobox.bean.EventBusBean;
import com.cuanbobox.bean.ImgBean;
import com.cuanbobox.cbavcx.R;
import com.cuanbobox.utils.ActivityUtil;
import com.cuanbobox.utils.DateUtil;
import com.cuanbobox.utils.LogUtil;
import com.cuanbobox.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ImgAdapter extends BaseQuickAdapter<ImgBean, BaseViewHolder> implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemLongClickListener {
    private int mode;

    public ImgAdapter() {
        super(R.layout.adapter_img_item, new ArrayList<ImgBean>());
        setOnItemClickListener(this);
        setOnItemLongClickListener(this);
    }

    @Override
    protected void convert(BaseViewHolder helper, ImgBean item) {
        ImageView ivSel=helper.getView(R.id.ivSel);
        ImageView ivImg=helper.getView(R.id.ivImg);
        Glide.with(ivImg).load(item.getFileName()).into(ivImg);
        helper.setText(R.id.tvName,""+item.getSimpleName());
        if(mode==0){
            ivSel.setVisibility(View.GONE);
        }else if(mode==1){//多选分享模式
            ivSel.setVisibility(View.VISIBLE);
        }
        if(item.isCheck()){
            ivSel.setImageResource(R.drawable.sel_ck);
        }else{
            ivSel.setImageResource(R.drawable.sel_unck);
        }
    }

    public void refurshData(String date){
        getData().removeAll(getData());
        File f=new File(App.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath() + "/capture/"+ date+"/");
        if(f.exists()&&f.isDirectory()){
            String[] strs=f.list();
            for (int i=0;i<strs.length;i++){
                getData().add(new ImgBean(App.getInstance().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath() + "/capture/"+ date+"/"+strs[i],strs[i]));
            }
        }
        notifyDataSetChanged();
    }

    public void setMode(int mode) {
        this.mode = mode;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ImgBean bean=getItem(position);
        if(MainActivity.isCompare){
            EventBusBean eventBusBean=new EventBusBean(EventBusBean.COMPARE);
            eventBusBean.setData(getItem(position).getFileName());
            EventBus.getDefault().post(eventBusBean);
        }else{
            if(mode==1){//分享
                App.shareImgList.add(bean);
                bean.setCheck(!bean.isCheck());
                notifyDataSetChanged();
            }else{
                ActivityUtil.open(mContext, PreviewActivity.class,"imgPath",bean.getFileName());
            }
        }
    }

    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        try{
            new File(getItem(position).getFileName()).delete();
            getData().remove(position);
            notifyDataSetChanged();
        }catch (Exception e){
            LogUtil.addError(e);
        }
        return true;
    }
}